package com.tony.demo.ui.base;

import android.content.Intent;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.puhui.lib.base.LoadingDialog;
import com.puhui.lib.base.RootActivity;
import com.puhui.lib.utils.DMLog;
import com.puhui.lib.utils.MemoryLeakUtils;
import com.puhui.lib.utils.MyToastUtil;
import com.puhui.lib.widgets.statusbar.StatusBarUtil;
import com.tony.demo.R;
import com.tony.demo.mvp.presenter.impl.BasePresenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Copyright:
 * Created by TangJian on 2019/4/17.
 * Modified:
 */
@SuppressWarnings("unused")
public abstract class BaseActivity<P extends BasePresenter> extends RootActivity {
    protected P presenter;
    private LoadingDialog loadingDialog;
    private View backView;
    /**
     * 保存上一次点击时间
     */
    private SparseArray<Long> lastClickTimes = new SparseArray<>();

    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = createPresenter();
        if (null != presenter) {
            presenter.attachView(this);
        }

        if (needEventNotify() && !EventBus.getDefault().isRegistered(this)) EventBus.getDefault().register(this);

        setContentView(getViewLayout());
        initView();
        initData();
    }

    protected boolean needEventNotify() {
        return false;
    }

    /**
     * 得到具体的presenter实例
     */
    protected abstract P createPresenter();

    /**
     * 添加界面布局
     */
    protected abstract int getViewLayout();

    protected void initView() {
    }

    protected void initData() {
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (null == backView) {
            backView = findViewById(R.id.btn_back);
            if (null != backView) {
                backView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
            }
        }
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(layoutResID);
        setStatusBar();
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        super.setContentView(view, params);
        setStatusBar();
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        setStatusBar();
    }

    /**
     * 设置状态栏
     */
    protected void setStatusBar() {
        StatusBarUtil.setColor(this, ContextCompat.getColor(this, R.color.main_color), 0);
    }

    /**
     * 检查是否可执行点击操作 防重复点击
     *
     * @return 返回true则可执行
     */
    public boolean checkClick(int id) {
        Long lastTime = lastClickTimes.get(id);
        Long thisTime = System.currentTimeMillis();
        lastClickTimes.put(id, thisTime);
        if (lastTime != null && thisTime - lastTime < 800) {
            // 快速双击，第二次不处理
            return false;
        } else {
            return true;
        }
    }

    /**
     * 如果不写一个方法，那么运行时会把错说当前类或父类中没有使用Subscribe注解的方法
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void doBaseEvent(EventBus temp) {
        DMLog.e(this.getClass().getSimpleName(), "temp");
    }

    @Override
    public void stopLoading() {
        if (loadingDialog == null) {
            loadingDialog = LoadingDialog.getInstance(this);
        }
        loadingDialog.dismiss();
    }

    protected void startActivity(Class clazz) {
        startActivity(new Intent(this, clazz));
    }

    @Override
    public void showToast(String toastContent) {
        MyToastUtil.getInstance().show(toastContent);
    }

    @Override
    public void showToast(int resourceId) {
        MyToastUtil.getInstance().show(resourceId);
    }

    @Override
    protected void onDestroy() {
        if (null != presenter) {
            presenter.detachView();
        }

        stopLoading();
        if (EventBus.getDefault().isRegistered(this)) EventBus.getDefault().unregister(this);
        MemoryLeakUtils.fixInputMethodManagerLeak(this);
        super.onDestroy();
    }
}
